% ************************************************************************ 
%     Module name   : Lynx Simulation - Search Mode Run                                                     
%                                                                          
%     Description   : This will run the Search mode.                                                    
%                                                                          
%     $Header: /Lynx/SimulationFramework/RSP/IFS/IFSRun.m 2     15/10/08 16:08 Lwabeke $ 
%                                                                          
%    $Author: Lwabeke $	            
%                                                                         
%    $Revision: 2 $                
%                                                                         
% ************************************************************************
%     $History: IFSRun.m $ 
% 
%
% ************************************************************************
%function [newTargets, SearchPerf, worldState] = SearchMode_Run(time, param, radarSetup, oldTargetList, worldState, SearchPerf)
function [newTargets] = SearchMode_Run(time, param, radarSetup, oldTargetList)
global perfHist;
global worldState;
foundTargets = 0;


sectorSize = param.radar.searchMaxAngle / param.radarModes.searchMode.numSearchSectors;

%% See if targets are detected
for tgtCntr=1:worldState.numTargets

   target = worldState.Target(tgtCntr);    
   %targetID = target.id;
   
   % See if target is already known, then don't process further
   oldMatch = target.found;
%    oldMatch = 0;
%    for oldtgtCntr=1:oldTargetList.numTargets
%      if (targetID == oldTargetList.Targets(oldtgtCntr).id)
%         oldMatch = 1; 
%      end
%    end
   
   if (oldMatch)
       continue;
   end
   
   if (time > target.initTime)&&(time < target.endTime)
       targetBearing = atan2(target.currentPos(1), target.currentPos(2) );
       targetSector = floor(mod(targetBearing,2*pi) / sectorSize) + 1;
       if (radarSetup.sector == targetSector)
           pd = probability_detect(radarSetup, target, param);
           perfHist.search.hist.radVel(perfHist.search.hist.numEntries,:) = [radialVel(target) targetSector];
           perfHist.search.hist.numEntries = perfHist.search.hist.numEntries + 1;
           
           if (rand(1) < pd)
               foundTargets = foundTargets+1;
               target.RangeEstStd = param.radarModes.searchMode.rangeEstStd;
               target.NumMeasurements = 1;
               newTargets(foundTargets) = target;
               worldState.Target(tgtCntr).found = 1;
           end
       end
   
   end
  
end

%% See if clutter spikes are detected
tgtCntr=worldState.oldestActive;
oldestActive = 0;

while ((tgtCntr<size(worldState.Target,2))&&(worldState.Target(tgtCntr).initTime < time))  % clutter sorted based on initTime, once we past this check, all remaining will be in the future

   target = worldState.Target(tgtCntr);    
   targetID = target.id;
   
   % See if target is already known, then don't process further
   oldMatch = target.found;
%    oldMatch = 0;
%    for oldtgtCntr=1:oldTargetList.numTargets
%      if (targetID == oldTargetList.Targets(oldtgtCntr).id)
%         oldMatch = 1; 
%      end
%    end
   
   if (oldMatch)
       tgtCntr = tgtCntr + 1;
       continue;
   end
   
   if (time < target.endTime) % &&(time > target.initTime)
       if (oldestActive == 0)
           oldestActive = tgtCntr;   % Keeps track of the oldest clutter index that was found to still be active
       end
   
       targetBearing = atan2(target.currentPos(1), target.currentPos(2) );
       targetSector = floor(mod(targetBearing,2*pi) / sectorSize) + 1;
       if (radarSetup.sector == targetSector)
           pd = probability_detect(radarSetup, target, param);
           perfHist.search.hist.radVel(perfHist.search.hist.numEntries,:) = [radialVel(target) targetSector];
           perfHist.search.hist.numEntries = perfHist.search.hist.numEntries + 1;
           
           if (rand(1) < pd)
               foundTargets = foundTargets+1;
               target.RangeEstStd = param.radarModes.searchMode.rangeEstStd;
               target.NumMeasurements = 1;
               newTargets(foundTargets) = target;
               worldState.Target(tgtCntr).found = 1;
           end
       end   
   end
  
   tgtCntr = tgtCntr + 1;
end

if  (oldestActive > 0)
    worldState.oldestActive  = oldestActive;
end

%% Check for false alarms
  %pfa(radarSetup)


%%
  if (~exist('newTargets','var'))
      newTargets = [];
  end

